package cn.lili.listener;

import cn.lili.modules.member.client.UnionLogClient;
import cn.lili.modules.member.entity.dos.UnionLog;
import cn.lili.routing.UnionRoutingKey;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class UnionLogListener {

    private final UnionLogClient unionLogClient;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${lili.amqp.union}" + "_" + UnionRoutingKey.ADD_UNION_LOG),
            exchange = @Exchange(value = "${lili.amqp.union}"),
            key = UnionRoutingKey.ADD_UNION_LOG))
    public void addUnionLog(UnionLog unionLog) {
        log.info("收到工会日志监听");
        try {
            unionLogClient.addUnionLog(unionLog);
        }catch (Exception e) {
            log.error("工会日志记录失败{}: ", unionLog.getId(), e);
        }

    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${lili.amqp.union}" + "_" + UnionRoutingKey.BATCH_ADD_UNION_LOG),
            exchange = @Exchange(value = "${lili.amqp.union}"),
            key = UnionRoutingKey.BATCH_ADD_UNION_LOG))
    public void addUnionLog(List<UnionLog> unionLogList) {
        log.info("收到工会日志批量监听：{}", unionLogList.size());
        try {
            if (!unionLogList.isEmpty()) {
                unionLogClient.batchAddUnionLog(unionLogList);
            }
        }catch (Exception e) {
            log.error("工会批量日志记录失败: ", e);
        }

    }
}
